using System;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ctsu.BaseForms;
using ctsu.Shared;

namespace ctsu.Admin.BusLogic
{

	public class BLSearchResultsBaseImpl : IBLSearchResultsBase
	{
		public event BusinessLogicEvents.BLSelectedItemEventHandler ItemSelected;
		protected DateTime _gridMouseDownTime;
		private bool _cancelClosing = false;
		private int _selectedID = -1;
		private FormSearchResultsBase _form;
		private DataTable _dt;
		private DataGrid _dg;

		public IBLSearchResultsBase BusinessLogic
		{
			get { return this; }
		}

		public BLSearchResultsBaseImpl(FormSearchResultsBase form, DataTable dt, DataGrid dg)
		{
			this._form = form;
			this._dt = dt;
			this._dg = dg;
		}

		public virtual bool Initialize()
		{
			this._form.Left = Utils.GetLeftCenterCoord(this._form.Width);
			this._form.Top = 10;
			//use a dataview for sorting purposes
			//DataView dv = new DataView(this._dt);
			StringBuilder sb = new StringBuilder();
			try
			{
				_dg.DataSource = this._dt;
				DataGridTableStyle dgts = new DataGridTableStyle();
				dgts.MappingName = this._dt.TableName;
				_dg.TableStyles.Add(dgts);

				int i = 0;
				foreach (DataColumn dc in this._dt.Columns)
				{
					dgts.GridColumnStyles[i].Width = Convert.ToInt32(dc.ExtendedProperties["Width"]);
					dgts.GridColumnStyles[i].HeaderText = dc.Caption;
					++i;
				}

				//add grid events
				_dg.MouseDown += new MouseEventHandler(_dg_MouseDown);
				_dg.MouseUp += new MouseEventHandler(_dg_MouseUp);
				_dg.CurrentCellChanged += new EventHandler(_dg_CurrentCellChanged);
				_dg.Select(0);
			}
			catch (Exception ex)
			{
				sb.Append("Could not initialize the search results form.");
				sb.Append(Environment.NewLine);
				sb.Append("Error: " + ex.Message);
				////ExceptionPolicy.HandleException(ex, "Exception Policy");
				MessageBox.Show(sb.ToString(), "Initialize Search Results Form Failed", MessageBoxButtons.OK, MessageBoxIcon.Stop);
				return false;
			}

			return true;
		}

		private void _dg_CurrentCellChanged(object sender, EventArgs e)
		{
			int row = _dg.CurrentCell.RowNumber;
			Console.WriteLine("dataGrid1_CurrentCellChanged, row:" + row);
			if (row > -1)
				_dg.Select(row);
		}

		private void _dg_MouseUp(object sender, MouseEventArgs e)
		{
			System.Drawing.Point pt = new Point(e.X, e.Y);
			DataGrid.HitTestInfo hti = _dg.HitTest(pt);

			if (hti.Type == DataGrid.HitTestType.Cell)
			{
				_dg.CurrentCell = new DataGridCell(hti.Row, hti.Column);
				_dg.Select(hti.Row);
			}
		}

		private void _dg_MouseDown(object sender, MouseEventArgs e)
		{
			DataGrid.HitTestInfo hti = _dg.HitTest(e.X, e.Y);

			if (hti.Type == DataGrid.HitTestType.Cell || hti.Type == DataGrid.HitTestType.RowHeader)
			{

				if (DateTime.Now < _gridMouseDownTime.AddMilliseconds(SystemInformation.DoubleClickTime))
				{
					//MessageBox.Show("GridDoubleClick");
					this.Accept(null, EventArgs.Empty);
				}

				_gridMouseDownTime = DateTime.Now;
				Console.WriteLine("dataGrid1_MouseDown  ");
			}
		}


		public bool CancelClosing
		{
			get
			{
				return _cancelClosing;
			}
			set
			{
				_cancelClosing = value;
			}
		}

		public System.Data.DataTable ResultsTable
		{
			get
			{
				return _dt;
			}
			set
			{
				_dt = value;
			}
		}

		public FormSearchResultsBase ResultsForm
		{
			get
			{
				return _form;
			}
			set
			{
				_form = value;
			}
		}

		public int SelectedID
		{
			get
			{
				return _selectedID;
			}
			set
			{
				_selectedID = value;
			}
		}


		#region ISearchResultsController Members

		public void GenericAction(object sender, EventArgs e)
		{
			Apply(sender, e);
		}

		#endregion

		#region IFormClose Members

		public void Cancel(object sender, EventArgs e)
		{

		}

		public void Apply(object sender, EventArgs e)
		{
			int row = _dg.CurrentRowIndex;
			if (row == -1)
			{
				MessageBox.Show(this._form, "You must select a row before pressing Apply.", "Select Row", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				return;
			}
			this.SelectedID = Convert.ToInt32(_dg[row, 0]);
			BLReturnIDEventArgs args = new BLReturnIDEventArgs("", SelectedID);
			if (ItemSelected != null)
				ItemSelected(this, args);

		}

		public void Accept(object sender, EventArgs e)
		{
			int row = _dg.CurrentRowIndex;
			if (row == -1)
			{
				MessageBox.Show(this._form, "You must select a row before pressing OK.", "Select Row", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				return;
			}
			this.SelectedID = Convert.ToInt32(_dg[row, 0]);
			BLReturnIDEventArgs args = new BLReturnIDEventArgs("", SelectedID);
			if (ItemSelected != null)
				ItemSelected(this, args);
			this._form.Close();
		}

		public void Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{

		}

		public void Closed(object sender, EventArgs e)
		{

		}

		public void Exit(object sender, EventArgs e)
		{
			this._form.Close();
		}

		#endregion


	}
}
